package leetcode.code0067;

import leetcode.helper.MaxDebug;

public class Solution extends Solution67 {

	@Override
	public String addBinary(String a, String b) {
		char[] na = a.toCharArray();
		char[] nb = b.toCharArray();
		int pa = na.length - 1;
		int pb = nb.length - 1;
		StringBuffer ans = new StringBuffer();
		int carry = 0;
		while (pa >= 0 && pb >= 0) {
			int num = na[pa--] - '0' + nb[pb--] - '0' + carry;
			ans.insert(0, num % 2);
			carry = num / 2;
		}
		while (pa >= 0) {
			int num = na[pa--] - '0' + carry;
			ans.insert(0, num % 2);
			carry = num / 2;
		}
		while (pb >= 0) {
			int num = nb[pb--] - '0' + carry;
			ans.insert(0, num % 2);
			carry = num / 2;
		}
		if (carry != 0) {
			ans.insert(0, carry);
		}
		return ans.toString();
	}

	public static void main(String[] args) {
		MaxDebug.debug14(new Solution());

	}

}
